package tech.aistar.day14;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 本类用来演示: 反射调用构造方法
 *
 * @author: success
 * @date: 2021/3/25 1:32 下午
 */
public class ConstructorDemo {
    public static void main(String[] args) {
        Class<?> c = Point.class;

        //1. 空参构造
        try {
            Point p = (Point) c.newInstance();

            //2. 根据指定的参数列表来获取指定的构造
            Constructor<?> c1 = c.getDeclaredConstructor();
            Point p2 = (Point) c1.newInstance();

            //3. 调用带参构造
            Constructor<?> c2 = c.getDeclaredConstructor(int.class,String.class);
            Point p3 = (Point) c2.newInstance(10,"admin");
            System.out.println(p3);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
